import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:toastification/toastification.dart';
import '../../utils/http.dart';
import 'components/home_list.dart';
import 'components/home_nav.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List notifyList = [];

  @override
  void initState() {
    super.initState();
    // print('111');
    getData();
    getNotifyList();
  }

  getData() async {
    final dio = Dio();
    // var response = await dio.get('https://live-api.itheima.net/announcement');
    // // print(response.data);
    // Future.delayed(const Duration(seconds: 2), () {
    //   toastification.show(
    //       context: context,
    //       title: response.toString(),
    //       autoCloseDuration: const Duration(seconds: 3),
    //       showProgressBar: false,
    //       showCloseButton: false,
    //       icon: const Icon(Icons.access_alarm_sharp),
    //       backgroundColor: Colors.greenAccent);
    // });
    // 与鸿蒙不同的点在于，dart中更推荐使用类构造对象而不是字面量对象
    // var res = await httpClass.request(requestParam: RequestParamModel(url: '/announcement'));
    // print(res.toString());
  }

  getNotifyList() async {
    var res = await httpClass.request(
        requestParam: RequestParamModel(url: '/announcement'));
    if (res['code'] != 10000) {
      toastification.showError(context: context, title: '数据获取失败');
    }
    setState(() {
      notifyList = res['data'];
      // print(notifyList);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color.fromARGB(40, 85, 145, 175),
        appBar: AppBar(
          title: const Text('享+社区'),
          backgroundColor: Colors.transparent,
          centerTitle: true,
        ),
        body: ListView(
          scrollDirection: Axis.vertical,
          padding: const EdgeInsets.all(10),
          children: [
            // 导航条
            HomeNav(),
            // 中间广告图
            ClipRRect(
              borderRadius: BorderRadius.circular(10.0),
              child: Image.asset('images/banner@2x.jpg'),
            ),
            // 社区公告
            HomeList(notifyList: notifyList),
          ],
        ));
  }
}
